// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 

package com.yoyo.lib.net;

import com.yoyo.lib.AppException;
import com.yoyo.lib.AppException.ErrorType;
import com.yoyo.lib.net.itf.IRequestListener;
import com.yoyo.lib.utilities.TextUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.util.*;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;

// Referenced classes of package com.stay.lib.net:
//            RequestInformation

public class HttpUrlUtil {
    
    private static final int TIMEOUT_CONNECTION = 15000;
    private static final int TIMEOUT_READ = 15000;

    private static void addHeader(HttpURLConnection paramHttpURLConnection, HashMap<String,String> paramHashmap) {
        if (null == paramHttpURLConnection || (paramHashmap.size() <= 0)) {
            return;
        }
        Iterator localIterator = paramHashmap.entrySet().iterator();
        while (localIterator.hasNext()) {
            Map.Entry localEntry = ( Entry ) localIterator.next();
            paramHttpURLConnection.addRequestProperty((String)localEntry.getKey(), (String)localEntry.getValue());
        }
    }

    public static HttpURLConnection execute(RequestInformation requestinformation) throws AppException {
        HttpURLConnection httpurlconnection = null;
        if (requestinformation.mRequestMethod.equals("GET")) {
            httpurlconnection = get(requestinformation);
        } else if (requestinformation.mRequestMethod.equals("POST")) {
            httpurlconnection = post(requestinformation);
        } else {
            throw new RuntimeException((new StringBuilder()).append("request method:")
                    .append(requestinformation.mRequestMethod).append(" is not supported").toString());
        }
        return httpurlconnection;
    }

    private static HttpURLConnection get(RequestInformation requestinformation) throws AppException {
        HttpURLConnection httpurlconnection = null;
        try {
            httpurlconnection = ( HttpURLConnection ) (new URL(requestinformation.url)).openConnection();
            httpurlconnection.setRequestMethod(requestinformation.mRequestMethod);
            httpurlconnection.setConnectTimeout(TIMEOUT_CONNECTION);
            httpurlconnection.setReadTimeout(TIMEOUT_READ);
            addHeader(httpurlconnection, requestinformation.headers);
        } catch (MalformedURLException malformedurlexception) {
            throw new AppException(ErrorType.ConnectionException, "MalformedURLException",
                    malformedurlexception.getMessage(), null);
        } catch (ProtocolException protocolexception) {
            throw new AppException(ErrorType.ConnectionException, "ProtocolException",
                    protocolexception.getMessage(), null);
        } catch (IOException ioexception) {
            throw new AppException(ErrorType.ConnectionException, "IOException", ioexception.getMessage(),
                    null);
        }
        return httpurlconnection;
    }

    private static String getParams(ArrayList<NameValuePair> paramArraylist) {
        String str = "";
        for (int i = 0; i < paramArraylist.size(); i++) {
            NameValuePair localNameValuePair = ( NameValuePair ) paramArraylist.get(i);
            str = str + localNameValuePair.getName() + "=" + localNameValuePair.getValue();
            if (i == -1 + paramArraylist.size()) {
                continue;
            }
            str = str + "&";
        }

        return str;
    }

    private static HttpURLConnection post(RequestInformation requestinformation)
        throws AppException
    {
        OutputStream outputstream;
        boolean flag;
        outputstream = null;
        flag = false;
        HttpURLConnection httpurlconnection;
        boolean flag1;
        httpurlconnection = (HttpURLConnection)(new URL(requestinformation.url)).openConnection();
        httpurlconnection.setRequestMethod(requestinformation.mRequestMethod);
        httpurlconnection.setUseCaches(false);
        httpurlconnection.setDoInput(true);
        httpurlconnection.setDoOutput(true);
        httpurlconnection.setConnectTimeout(TIMEOUT_CONNECTION);
        httpurlconnection.setReadTimeout(TIMEOUT_READ);
        addHeader(httpurlconnection, requestinformation.headers);
        outputstream = httpurlconnection.getOutputStream();
        if(TextUtil.isValidate(requestinformation.urlParameters)){
            outputstream.write(getParams(requestinformation.urlParameters).getBytes());
        }
        if(TextUtil.isValidate(requestinformation.postContent)) {
            outputstream.write(requestinformation.postContent.getBytes());
        }
        if(requestinformation.byteParams != null) {
            outputstream.write(requestinformation.byteParams);
        }
        if(requestinformation.mRequestListener == null) {
//            break MISSING_BLOCK_LABEL_163;
        }
        flag1 = requestinformation.mRequestListener.onPrepareParams(outputstream);
        flag = flag1;
        if(outputstream != null && !flag)
            try
            {
                outputstream.flush();
                outputstream.close();
            }
            catch(IOException ioexception2)
            {
                ioexception2.printStackTrace();
            }
        return httpurlconnection;
        MalformedURLException malformedurlexception;
        malformedurlexception;
        throw new AppException(ErrorType.ConnectionException, "MalformedURLException", malformedurlexception.getMessage(), null);
        Exception exception;
        exception;
        IOException ioexception;
        ProtocolException protocolexception;
        if(outputstream != null && true)
            try
            {
                outputstream.flush();
                outputstream.close();
            }
            catch(IOException ioexception1)
            {
                ioexception1.printStackTrace();
            }
        throw exception;
        protocolexception;
        throw new AppException(ErrorType.ConnectionException, "ProtocolException", protocolexception.getMessage(), null);
        ioexception;
        throw new AppException(ErrorType.ConnectionException, "IOException", ioexception.getMessage(), null);
    }
}
